﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class ePioneer_UserInfo
    {
        readonly LYM.CommonWeb.DataServer.DataBase data = new LYM.CommonWeb.DataServer.DataBase(LYM.CommonWeb.ConfigHelper.GetConfigString("EMPC"), false);
        public ePioneer_UserInfo()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.ePioneer_UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ePioneer_UserInfo(");
            strSql.Append("UserId,LoginName,PassWord,UserName,Mobile,Email)");
            strSql.Append(" values (");
            strSql.Append("@UserId,@LoginName,@PassWord,@UserName,@Mobile,@Email)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.NVarChar,50),
					new SqlParameter("@LoginName", SqlDbType.NVarChar,50),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,50),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.UserId == null ? "" : model.UserId;
            parameters[1].Value = model.LoginName == null ? "" : model.LoginName;
            parameters[2].Value = model.PassWord == null ? "" : model.PassWord;
            parameters[3].Value = model.UserName == null ? "" : model.UserName;
            parameters[4].Value = model.Mobile == null ? "" : model.Mobile;
            parameters[5].Value = model.Email == null ? "" : model.Email;

            object obj = null;
            data.GetValue(strSql.ToString(), false, ref obj, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ePioneer_UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ePioneer_UserInfo set ");
            strSql.Append("UserId=@UserId,");
            strSql.Append("LoginName=@LoginName,");
            strSql.Append("PassWord=@PassWord,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Email=@Email");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.NVarChar,50),
					new SqlParameter("@LoginName", SqlDbType.NVarChar,50),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,50),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,100),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.UserId == null ? "" : model.UserId;
            parameters[1].Value = model.LoginName == null ? "" : model.LoginName;
            parameters[2].Value = model.PassWord == null ? "" : model.PassWord;
            parameters[3].Value = model.UserName == null ? "" : model.UserName;
            parameters[4].Value = model.Mobile == null ? "" : model.Mobile;
            parameters[5].Value = model.Email == null ? "" : model.Email;
            parameters[6].Value = model.Id;

            int rows = 0;
            data.ProExecuteCommand(strSql.ToString(), false, ref rows, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ePioneer_UserInfo GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,UserId,LoginName,PassWord,UserName,Mobile,Email from ePioneer_UserInfo ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            Model.ePioneer_UserInfo model = new Model.ePioneer_UserInfo();
            DataTable dt = new DataTable();
            data.GetDataTable(strSql.ToString(), false,ref dt, parameters);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["Id"] != null && dt.Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(dt.Rows[0]["Id"].ToString());
                }
                if (dt.Rows[0]["UserId"] != null && dt.Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = dt.Rows[0]["UserId"].ToString();
                }
                if (dt.Rows[0]["LoginName"] != null && dt.Rows[0]["LoginName"].ToString() != "")
                {
                    model.LoginName = dt.Rows[0]["LoginName"].ToString();
                }
                if (dt.Rows[0]["PassWord"] != null && dt.Rows[0]["PassWord"].ToString() != "")
                {
                    model.PassWord = dt.Rows[0]["PassWord"].ToString();
                }
                if (dt.Rows[0]["UserName"] != null && dt.Rows[0]["UserName"].ToString() != "")
                {
                    model.UserName = dt.Rows[0]["UserName"].ToString();
                }
                if (dt.Rows[0]["Mobile"] != null && dt.Rows[0]["Mobile"].ToString() != "")
                {
                    model.Mobile = dt.Rows[0]["Mobile"].ToString();
                }
                if (dt.Rows[0]["Email"] != null && dt.Rows[0]["Email"].ToString() != "")
                {
                    model.Email = dt.Rows[0]["Email"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        #endregion  Method

        #region 自定义方法
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public Model.ePioneer_UserInfo Login(string loginName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,UserId,LoginName,PassWord,UserName,Mobile,Email from ePioneer_UserInfo ");
            strSql.Append(" where LoginName=@LoginName");
            SqlParameter[] parameters = {
                                            new SqlParameter("@LoginName",loginName)
                                        };

            Model.ePioneer_UserInfo model = new Model.ePioneer_UserInfo();
            DataTable dt = new DataTable();
            data.GetDataTable(strSql.ToString(), false,ref dt, parameters);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["Id"] != null && dt.Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(dt.Rows[0]["Id"].ToString());
                }
                if (dt.Rows[0]["UserId"] != null && dt.Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = dt.Rows[0]["UserId"].ToString();
                }
                if (dt.Rows[0]["LoginName"] != null && dt.Rows[0]["LoginName"].ToString() != "")
                {
                    model.LoginName = dt.Rows[0]["LoginName"].ToString();
                }
                if (dt.Rows[0]["PassWord"] != null && dt.Rows[0]["PassWord"].ToString() != "")
                {
                    model.PassWord = dt.Rows[0]["PassWord"].ToString();
                }
                if (dt.Rows[0]["UserName"] != null && dt.Rows[0]["UserName"].ToString() != "")
                {
                    model.UserName = dt.Rows[0]["UserName"].ToString();
                }
                if (dt.Rows[0]["Mobile"] != null && dt.Rows[0]["Mobile"].ToString() != "")
                {
                    model.Mobile = dt.Rows[0]["Mobile"].ToString();
                }
                if (dt.Rows[0]["Email"] != null && dt.Rows[0]["Email"].ToString() != "")
                {
                    model.Email = dt.Rows[0]["Email"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}
